import React, { lazy } from 'react';
import { RouteObject } from 'react-router-dom';
import { AppRoute, PageMeta } from '@/types';
import { ProtectedRoute } from '@/components/common/ProtectedRoute';

// 懒加载页面组件
const DashboardPage = lazy(() => import('@/pages/dashboard/DashboardPage'));
const LoginPage = lazy(() => import('@/pages/auth/LoginPage'));

// 租户管理页面
const TenantListPage = lazy(() => import('@/pages/tenant/TenantListPage'));
const TenantCreatePage = lazy(() => import('@/pages/tenant/TenantCreatePage'));
const TenantEditPage = lazy(() => import('@/pages/tenant/TenantEditPage'));
const TenantPlansPage = lazy(() => import('@/pages/tenant/TenantPlansPage'));

// 组织管理页面
const OrganizationListPage = lazy(() => import('@/pages/organization/OrganizationListPage'));
const OrganizationCreatePage = lazy(() => import('@/pages/organization/OrganizationCreatePage'));
const OrganizationEditPage = lazy(() => import('@/pages/organization/OrganizationEditPage'));

// 用户管理页面
const UserListPage = lazy(() => import('@/pages/user/UserListPage'));
const UserCreatePage = lazy(() => import('@/pages/user/UserCreatePage'));
const UserEditPage = lazy(() => import('@/pages/user/UserEditPage'));

// 角色权限管理页面
const RoleListPage = lazy(() => import('@/pages/role/RoleListPage'));
const RoleCreatePage = lazy(() => import('@/pages/role/RoleCreatePage'));
const RoleEditPage = lazy(() => import('@/pages/role/RoleEditPage'));
const PermissionListPage = lazy(() => import('@/pages/role/PermissionListPage'));

// 部门岗位管理页面
const DepartmentListPage = lazy(() => import('@/pages/department/DepartmentListPage'));
const DepartmentCreatePage = lazy(() => import('@/pages/department/DepartmentCreatePage'));
const DepartmentEditPage = lazy(() => import('@/pages/department/DepartmentEditPage'));
const PositionListPage = lazy(() => import('@/pages/position/PositionListPage'));
const PositionCreatePage = lazy(() => import('@/pages/position/PositionCreatePage'));
const PositionEditPage = lazy(() => import('@/pages/position/PositionEditPage'));

// 系统设置页面
const SystemSettingsPage = lazy(() => import('@/pages/settings/SystemSettingsPage'));
const TenantSettingsPage = lazy(() => import('@/pages/settings/TenantSettingsPage'));
const OrganizationSettingsPage = lazy(() => import('@/pages/settings/OrganizationSettingsPage'));
const AuditLogsPage = lazy(() => import('@/pages/settings/AuditLogsPage'));

// 配置中心页面
const ConfigCenterPage = lazy(() => import('@/pages/config/ConfigCenterPage'));

// 404页面
const NotFoundPage = lazy(() => import('@/pages/common/NotFoundPage'));

// 页面元信息
const pagesMeta: Record<string, PageMeta> = {
  dashboard: {
    title: '仪表板',
    description: '系统概览和统计信息',
    permissions: ['dashboard.view'],
    breadcrumbs: [
      { label: '首页', path: '/' },
    ],
  },
  tenants: {
    title: '租户管理',
    description: '管理系统中的所有租户',
    permissions: ['tenant.view'],
    breadcrumbs: [
      { label: '首页', path: '/' },
      { label: '租户管理' },
    ],
  },
  'tenant-create': {
    title: '创建租户',
    description: '创建新的租户',
    permissions: ['tenant.create'],
    breadcrumbs: [
      { label: '首页', path: '/' },
      { label: '租户管理', path: '/tenants' },
      { label: '创建租户' },
    ],
  },
  'tenant-edit': {
    title: '编辑租户',
    description: '编辑租户信息',
    permissions: ['tenant.edit'],
    breadcrumbs: [
      { label: '首页', path: '/' },
      { label: '租户管理', path: '/tenants' },
      { label: '编辑租户' },
    ],
  },
  organizations: {
    title: '组织管理',
    description: '管理组织架构',
    permissions: ['organization.view'],
    breadcrumbs: [
      { label: '首页', path: '/' },
      { label: '组织管理' },
    ],
  },
  users: {
    title: '用户管理',
    description: '管理系统用户',
    permissions: ['user.view'],
    breadcrumbs: [
      { label: '首页', path: '/' },
      { label: '用户管理' },
    ],
  },
  roles: {
    title: '角色管理',
    description: '管理用户角色',
    permissions: ['role.view'],
    breadcrumbs: [
      { label: '首页', path: '/' },
      { label: '角色管理' },
    ],
  },
  permissions: {
    title: '权限管理',
    description: '管理系统权限',
    permissions: ['permission.view'],
    breadcrumbs: [
      { label: '首页', path: '/' },
      { label: '权限管理' },
    ],
  },
  departments: {
    title: '部门管理',
    description: '管理组织部门',
    permissions: ['department.view'],
    breadcrumbs: [
      { label: '首页', path: '/' },
      { label: '部门管理' },
    ],
  },
  positions: {
    title: '岗位管理',
    description: '管理部门岗位',
    permissions: ['position.view'],
    breadcrumbs: [
      { label: '首页', path: '/' },
      { label: '岗位管理' },
    ],
  },
  settings: {
    title: '系统设置',
    description: '系统配置和设置',
    permissions: ['system.view'],
    breadcrumbs: [
      { label: '首页', path: '/' },
      { label: '系统设置' },
    ],
  },
  config: {
    title: '配置中心',
    description: '系统配置管理',
    permissions: ['config.view'],
    breadcrumbs: [
      { label: '首页', path: '/' },
      { label: '配置中心' },
    ],
  },
};

// 创建受保护的路由组件
const createProtectedRoute = (
  Component: React.ComponentType,
  meta?: PageMeta
) => {
  return (
    <ProtectedRoute
      guard={{
        requireAuth: true,
        permissions: meta?.permissions,
        roles: meta?.roles,
      }}
    >
      <Component />
    </ProtectedRoute>
  );
};

// 路由配置
export const routeConfig: RouteObject[] = [
  // 认证路由
  {
    path: '/login',
    element: <LoginPage />,
  },
  
  // 主应用路由
  {
    path: '/',
    children: [
      // 仪表板
      {
        index: true,
        element: createProtectedRoute(DashboardPage, pagesMeta.dashboard),
      },
      {
        path: 'dashboard',
        element: createProtectedRoute(DashboardPage, pagesMeta.dashboard),
      },
      
      // 租户管理
      {
        path: 'tenants',
        children: [
          {
            index: true,
            element: createProtectedRoute(TenantListPage, pagesMeta.tenants),
          },
          {
            path: 'create',
            element: createProtectedRoute(TenantCreatePage, pagesMeta['tenant-create']),
          },
          {
            path: ':id/edit',
            element: createProtectedRoute(TenantEditPage, pagesMeta['tenant-edit']),
          },
          {
            path: 'plans',
            element: createProtectedRoute(TenantPlansPage),
          },
        ],
      },
      
      // 组织管理
      {
        path: 'organizations',
        children: [
          {
            index: true,
            element: createProtectedRoute(OrganizationListPage, pagesMeta.organizations),
          },
          {
            path: 'create',
            element: createProtectedRoute(OrganizationCreatePage),
          },
          {
            path: ':id/edit',
            element: createProtectedRoute(OrganizationEditPage),
          },
        ],
      },
      
      // 用户管理
      {
        path: 'users',
        children: [
          {
            index: true,
            element: createProtectedRoute(UserListPage, pagesMeta.users),
          },
          {
            path: 'create',
            element: createProtectedRoute(UserCreatePage),
          },
          {
            path: ':id/edit',
            element: createProtectedRoute(UserEditPage),
          },
        ],
      },
      
      // 角色管理
      {
        path: 'roles',
        children: [
          {
            index: true,
            element: createProtectedRoute(RoleListPage, pagesMeta.roles),
          },
          {
            path: 'create',
            element: createProtectedRoute(RoleCreatePage),
          },
          {
            path: ':id/edit',
            element: createProtectedRoute(RoleEditPage),
          },
        ],
      },
      
      // 权限管理
      {
        path: 'permissions',
        element: createProtectedRoute(PermissionListPage, pagesMeta.permissions),
      },
      
      // 部门管理
      {
        path: 'departments',
        children: [
          {
            index: true,
            element: createProtectedRoute(DepartmentListPage, pagesMeta.departments),
          },
          {
            path: 'create',
            element: createProtectedRoute(DepartmentCreatePage),
          },
          {
            path: ':id/edit',
            element: createProtectedRoute(DepartmentEditPage),
          },
        ],
      },
      
      // 岗位管理
      {
        path: 'positions',
        children: [
          {
            index: true,
            element: createProtectedRoute(PositionListPage, pagesMeta.positions),
          },
          {
            path: 'create',
            element: createProtectedRoute(PositionCreatePage),
          },
          {
            path: ':id/edit',
            element: createProtectedRoute(PositionEditPage),
          },
        ],
      },
      
      // 系统设置
      {
        path: 'settings',
        children: [
          {
            index: true,
            element: createProtectedRoute(SystemSettingsPage, pagesMeta.settings),
          },
          {
            path: 'tenant',
            element: createProtectedRoute(TenantSettingsPage),
          },
          {
            path: 'organization',
            element: createProtectedRoute(OrganizationSettingsPage),
          },
          {
            path: 'logs',
            element: createProtectedRoute(AuditLogsPage),
          },
        ],
      },
      
      // 配置中心
      {
        path: 'config',
        element: createProtectedRoute(ConfigCenterPage, pagesMeta.config),
      },
      
      // 404页面
      {
        path: '*',
        element: <NotFoundPage />,
      },
    ],
  },
];

// 导出页面元信息
export { pagesMeta };